<?php

/**
 * 鸡蛋白活动页面
 * 编写人   陈泽富
*/

define('IN_ECS', true);
define('IS_MOBILE', true);
require(dirname(__FILE__) . '/../includes/init.php');
require_once(ROOT_PATH . '/includes/funGlair.php');
@ini_set('display_errors',        1);
!empty($_REQUEST['act']) || $_REQUEST['act'] = '';
/*领取鸡蛋白*/
if($_REQUEST['act'] == 'get_glair'){
    $return = array('error'=>0,'message'=>'','content'=>'');
    $return = get_glair();
    die(JSON()->encode($return, false));
}

/*领取鸡蛋白包邮券*/
if($_REQUEST['act'] =='get_glairBaoyou'){
    $return = array('error'=>0,'message'=>'','content'=>'');
    $return = get_glairBaoyou();
    die(JSON()->encode($return, false));
}

/*兑换鸡蛋白*/
if($_REQUEST['act'] == 'exchange'){
    $return=array('error'=>0,'message'=>'','content'=>'');
    $code = !empty($_REQUEST['code'])?$_REQUEST['code']:'0';
    $return = exchangeGlair($code);
    die(JSON()->encode($return));
}

//提交评论
if($_REQUEST['act'] == 'comment'){
        require(ROOT_PATH . 'includes/cls_json.php');
        $json   = new JSON;
        $user_id = !empty($_SESSION['user_id'])?$_SESSION['user_id']:'';
        $comment = !empty($_REQUEST['comment'])?$_REQUEST['comment']:'';
        $time=time();
        $return = array('error' => 0,'message' => '','content'=>'' );
        //查询用户是否评论
        $sql = "SELECT comment_id FROM ". $ecs->table('comment_white') ."WHERE user_id = $user_id ";
        $comment_id = $db->getOne($sql);
        if($comment_id){
            $return['error']=2;
            $return['message']='每人只能评论一次';
            echo $json->encode($return);
            exit();
        }

        $sql = 'INSERT INTO ' . $ecs->table('comment_white') . ' ( comment_content,user_id,add_time) ' .
               "VALUES('$comment',$user_id,$time)";
        $db->query($sql);
        if($db->insert_id()){
            $return['message']='评论成功';

            /*查询人信息内容*/
            $sql =  " SELECT u.head_url,us.user_name,u.sex FROM ".
                    $ecs->table('users') ." AS us ".
                    " LEFT JOIN ".$ecs->table('user_info') ." AS u ON u.user_id = us.user_id ".
                    " WHERE us.user_id = '$user_id' ";
            $user_info = $db->getRow($sql);
            $user_info['head_url'] = !empty($user_info['head_url'])?$user_info['head_url']:'img/sex'.$value['sex'].'.jpg';
            $content .='<div class="box">
              <img src="./../'.$user_info['head_url'].'" alt="头像">
              <span class="user_name">'.$user_info['user_name'].'</span><br />
              <p>'.$comment.'</p>
            </div>';
            $return['content']=$content;
            echo $json->encode($return);
            exit();
        }

        $return['error']=2;
        $return['content']='每人只能评论一次';
        echo $json->encode($return);
        exit();
}
//获取更多评论信息
if($_REQUEST['act'] == 'more_comment'){
    require(ROOT_PATH . 'includes/cls_json.php');
    $json   = new JSON;
    $return = array('error' => 0,'message'=>'','content'=>'' );
    $pages  = !empty($_REQUEST['pages'])?$_REQUEST['pages']:'1';
    $size   = "5";
    $start  = ($pages - 1) * $size + 3;
    //获取评论
    $sql =" SELECT c.comment_content ,c.comment_id , c.add_time, c.agree_num ,c.head_url,c.user_name FROM ".
          $ecs->table('comment_white') ." AS c ".
          " LIMIT $start , $size";
    $comment_list =$db->getall($sql);
    foreach ($comment_list as $key => $value) {
        $comment_list[$key]['head_url'] = !empty($value['head_url'])?$value['head_url']:'img/sex1.jpg';
        $comment_list[$key]['add_time'] = date("Y-m-d h:m:s",$value['add_time']);
    }
    $smarty->assign('comment_list',           $comment_list);
    if(!empty($comment_list)){
        $content = '';
        foreach ($comment_list as $key => $value) {
           $content .='<div class="box">
              <img src="./../'.$value['head_url'].'" alt="头像">
              <span class="user_name">'.$value['user_name'].'</span><br />
              <p>
                '.$value['comment_content'].'
              </p>
            </div>';
        }
        $return['content'] = $content;
    }
    else{
        $return['error']   = 2;
        $return['message'] = "已加载全部评论";
    }
    echo $json->encode($return);
    exit();
}

/*活动规则*/
if($_REQUEST['act'] == 'notice'){
    $smarty->assign('top_title', "活动规则");
    $smarty->display('glair/glair_notice.dwt');
    exit();
}

/* 开始页面显示 */
assign_template();
$time = time();
/* 模板文案 */
if ($time < strtotime('2015-09-19')) {
    $smarty->assign('pici', '第一批');
    $smarty->assign('startDate', date('m月d日', strtotime('2015-09-18')));
    $smarty->assign('startDate2', date('Y年m月d日', strtotime('2015-09-19')));
} elseif ($time < strtotime('2015-09-20')) {
    $smarty->assign('pici', '第二批');
    $smarty->assign('startDate', date('m月d日', strtotime('2015-09-19')));
    $smarty->assign('startDate2', date('Y年m月d日', strtotime('2015-09-20')));
} elseif ($time < strtotime('2015-09-21')) {
    $smarty->assign('pici', '第三批');
    $smarty->assign('startDate', date('m月d日', strtotime('2015-09-20')));
    $smarty->assign('startDate2', date('Y年m月d日', strtotime('2015-09-21')));
} elseif ($time < strtotime('2015-09-22')) {
    $smarty->assign('pici', '第四批');
    $smarty->assign('startDate', date('m月d日', strtotime('2015-09-21')));
    $smarty->assign('startDate2', date('Y年m月d日', strtotime('2015-09-22')));
} elseif ($time >= strtotime('2015-09-22')) {
    $smarty->assign('pici', '第五批');
    $smarty->assign('startDate', date('m月d日', strtotime('2015-09-22')));
    $smarty->assign('startDate2', date('Y年m月d日', strtotime('2015-09-23')));
}

//获取评论
$sql =" SELECT c.comment_content ,c.comment_id , c.add_time, c.agree_num ,c.head_url,c.user_name FROM ".
      $ecs->table('comment_white') ." AS c ".
      " LIMIT 0 , 5";
$comment_list= $db->getall($sql);

foreach ($comment_list as $key => $value) {
    $comment_list[$key]['head_url'] = !empty($value['head_url'])?$value['head_url']:'img/sex'.$value['sex'].'.jpg';
    $comment_list[$key]['add_time'] = date("Y-m-d h:m:s",$value['add_time']);
}
//获取评论总数
$sql = " SELECT count(comment_id) FROM ".$ecs->table('comment_white') ;
$comment_count = $db->getOne($sql);

$comment_count +=126;
//获取用户是否显示分享领取按钮
$user_id = !empty($_SESSION['user_id'])?$_SESSION['user_id']:'';
$smarty->assign('comment_count',           ($comment_count+1));
$smarty->assign('comment_list',           $comment_list);

/*读取亏损金额*/
$smarty->assign('top_title', "鸡蛋白");
$smarty->display('glair/glair_fans.dwt');

